package interfaz;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
/*
 * iMenu.java
 *
 * Created on 22 de febrero de 2008, 18:18
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author victor
 */
public class iTextMenu extends iWidget {
    private ArrayList items;
    private Font font;
    private Color activeColor;
    private Color unactiveColor;
    private int maxChars = 15;
    private int activeItem = 0;
    private iText folderIcon;
    private ArrayList folders = new ArrayList();
    
    /** Creates a new instance of iMenu */
    public iTextMenu(int x, int y) {
        super(x,y);
        items = new ArrayList();
        folderIcon = new iText(x-72,y);
        folderIcon.setText("*");
    }
    
    public void setMaxChars(int max) {
        this.maxChars = max;
    }
    
    public int getMaxChars() {
        return this.maxChars;
    }
    
    public void setFont(Font font) {
        this.font = font;
    }
    
    public void setActiveItem(int item) {
        this.activeItem = item;
    }
    
    public int getActiveItem() {
        return this.activeItem;
    }
    
    public String getActiveItemValue() {
        return (((iText) items.get(getActiveItem())).getText());
    }
        
    public boolean activeIsFolder () {
        return (((Integer) folders.get(getActiveItem())) == 1);
    } 
    
    public void decreaseActiveItem() {
        if (getActiveItem() > 0) setActiveItem(getActiveItem()-1);
    }
    
    public void increaseActiveItem() {
        if (getActiveItem() < size()-1) setActiveItem(getActiveItem()+1);
    }
    
    public Font getFont() {
        return this.font;
    }
    
    public void setActiveColor(Color color) {
        this.activeColor = color;
    }
    
    public Color getActiveColor() {
        return this.activeColor;
    }
    
    public void setUnActiveColor(Color color) {
        this.unactiveColor = color;
    }
    
    public Color getUnActiveColor() {
        return this.unactiveColor;
    }
    
    public void addItem(String item) {
        addItem(item,false);
    }
    
    public void addItem(String item,boolean folder) {
        iText text = new iText(getX(),getY());
        text.setText(item);
        text.setFont(font);
        text.setColor(getUnActiveColor());
        text.setMaxChars(getMaxChars());
        items.add(text);
        folders.add(items.indexOf(text),
                (folder == true) ? new Integer(1) : new Integer(0));
    }
    
    public iText getItem(int index) {
        return (iText) items.get(index);
    }
    
    public void delItem(int index) {
        items.remove(index);
    }
    
    public int size() {
        return items.size();
    }
    
    public int getDisplayableMenuSize() { //numero de items mostrables
        int DisplayableMenuSize = getHeight()/(font.getSize()+10);
        if (DisplayableMenuSize > size()) return size();
        return DisplayableMenuSize;
    }
    
    public int getMinIndex() { //devolve a posicion do elto que se ve primeiro
        //na pantalla
        if (getActiveItem() <= getDisplayableMenuSize()/2) return 0;
        else if (getActiveItem() >= size()-getDisplayableMenuSize()/2)
            return (size()-getDisplayableMenuSize());
        else return getActiveItem()-getDisplayableMenuSize()/2;
        //intentaremos que o cursor sempre estea no medio
    }
    
    public int getMaxIndex() {
        return getMinIndex() + getDisplayableMenuSize();
    }
    
    
    public void paint(Graphics g) {
        iText text;
        
        if (getWidth() == -1) setWidth((getMaxChars()+5)*font.getSize()/2);
        if (getHeight() == -1) setHeight((size()+1)*font.getSize());
        folderIcon.setFont(getFont());
        //calculo para conhecer as dimesions do menu. totalmente a ollo
        
        for (int i=0;i < getDisplayableMenuSize();i++) {
            text = (iText) getItem(getMinIndex()+i);
            folderIcon.setY(getY()+i*font.getSize()+5);
            text.setY(getY()+i*font.getSize()+5);
            folderIcon.setColor(getUnActiveColor());
            text.setColor(getUnActiveColor());
            if (getMinIndex()+i == getActiveItem()) {
                text.setColor(getActiveColor());
                folderIcon.setColor(getActiveColor());
            }
            if (((Integer) folders.get(getMinIndex()+i)) == 1)
            folderIcon.paint(g);
                text.paint(g);
        }
//        g.drawRect(getX()-20,getY()-5-font.getSize(),getWidth(),getHeight());
    }
    
    
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP: {
                decreaseActiveItem();
                break;
            }
            case KeyEvent.VK_DOWN: {
                increaseActiveItem();
                break;
            }
        }
    }
}
